﻿using System;
using System.Collections.Generic;
using System.Linq;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.Mtbl;

namespace WebApplication.UserControl
{
    public partial class UserMessageControl : System.Web.UI.UserControl
    {
        protected  string custUID=string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                BindControl_level1();
            }
        }

        private void BindControl_level1()
        {
            //建议不要直接使用Request[]提取信息 服务器会依次检索所有的QueryString、Form、Cookies...            
            string code =custUID= Request.QueryString[Constant.UrlParamName.CustomerUID];
            if (!string.IsNullOrEmpty(custUID))
            {
                tblCustomer ent = new tblCustomer();
                tblCustomerBLL bll = tblCustomerBLL.GetInstance();
                int customeruid = 0;
                int.TryParse(custUID, out customeruid);
                ent.CustomerUID=customeruid;
                ent = bll.GetEntity(ent);
                if (ent != null)
                {
                    if (ent.Deleted == Constant.IsDelete.yes) {
                        //如果该Customer 无效的话，跳转到 回收站
                        Response.Redirect("~/Recyclebin.aspx");
                    }

                    //绑定字段
                    ltlCompanyName_eng.Text = ent.Name_Eng;
                    ltlCompanyName_chi.Text = ent.Name_Chi;
                    lblCustomerID.Text = ent.CustomerID;
                    lblCustomerType.Text = mtblCustTypebBLL.GetInstance().GetCustType(ent.CustTypeUID);
                    lblMembershipStatus.Text = GetMemberStatusOrCategory(ent.CustomerUID,1);
                    lblMembershipCategory.Text = GetMemberStatusOrCategory(ent.CustomerUID, 2);

                    //绑定图片
                    tblMediaRoom_Related entMediaRoomRelatedParam = new tblMediaRoom_Related();
                    entMediaRoomRelatedParam.RelatedUID=ent.CustomerUID;
                    List<tblMediaRoom_Related> listMediaRoom_Related = tblMediaRoomRelatedBLL.GetInstance().GetListByENT(entMediaRoomRelatedParam);
                    if (listMediaRoom_Related.Count() > 0)
                    {
                        tblMediaRoom entMediaRoom = tblMediaRoomBLL.GetInstance().GetEntity(int.Parse(listMediaRoom_Related[0].MediaRoomUID.ToString()));
                        if(entMediaRoom!=null)
                            imgLogo.ImageUrl = "~"+entMediaRoom.FilePath + entMediaRoom.FileName + "_100_100.jpg";
                    }
                    
                }
            }
        }

        private tblCust_Member GetMemberByCustomerUID(int customerUID)
        {
            tblCust_Member entMember = new tblCust_Member();
            entMember.CustomerUID = customerUID;
            List<tblCust_Member> listMember = new List<tblCust_Member>();
            listMember = tblCust_MemberBLL.GetInstance().GetListByENT(entMember);
            if (listMember.Count() > 0)
                entMember = listMember[0];
            else
                entMember = null;
            return entMember;
        }

        /// <summary>
        /// 获取会员的 状态 或者 类别； type参数 1是获取类别； 2是获取状态
        /// </summary>
        /// <param name="customerUID"></param>
        /// <param name="type">1是获取会员的类别； 2是获取会员de等级</param>
        /// <returns></returns>
        private string GetMemberStatusOrCategory(int customerUID,int type)
        {
            string ret = string.Empty;
            tblCust_Member entMember = GetMemberByCustomerUID(customerUID);
            if (entMember != null)
            {
                if (type == 1)
                {
                    int tmp_uid = int.Parse(entMember.MemStatusUID.ToString());
                    ret = mtblMembershipStatusBLL.GetInstance().GetMembershipStatus(tmp_uid);
                }
                else if (type == 2)
                {
                    ret = mtblMembershipCategoryBLL.GetInstance().GetMemCategoryByMemberUID(entMember.MemberUID);
                }
            }
            return ret;
        }

        protected void lbtnDeleteCustomer_Click(object sender, EventArgs e)
        {
            custUID = Request.QueryString[Constant.UrlParamName.CustomerUID];
            if (!string.IsNullOrEmpty(custUID))
            {
                int ret = tblCustomerBLL.GetInstance().UpdateDeleted(int.Parse(custUID));
                if (ret > 0)
                {
                    Response.Redirect("~/CustomerList.aspx");
                }
            }
        }
        
    }
}